package otherOrganizations;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;

import agentsSME.MsgSender;
import agentsSME.Networking;
import agentsSME.SME;
import repast.simphony.engine.schedule.ScheduledMethod;
import repast.simphony.essentials.RepastEssentials;
import enviornment.FloodWarning;
import enviornment.Flood;
import enviornment.Location;
import main.BuildModel;

/**
 * Defines Environment Agency.
 * @author Chunhui Li
 *
 */
public class EAagent extends GovernmentAgent{
	private HashMap<Double, FloodWarning> alertTable;
	private MsgSender sender;
	private Networking net;	  
	// these arraylists are created to hold locations where the flood reach a certain
	// level. Flood warnings are generated based on counting how many locations are flooded.
//	ArrayList<Location> notFloodedLocations = new ArrayList<Location>();
//	ArrayList<Location> lightFloodedLocations = new ArrayList<Location>();
//	ArrayList<Location> mediumFloodedLocations = new ArrayList<Location>();
//	ArrayList<Location> heavyFloodedLocations = new ArrayList<Location>();
    
	/**
     * The constructor of EA agent.
     */
	public EAagent() {
		sender= new MsgSender();
		alertTable = new HashMap<Double, FloodWarning>();
	}
	
	public MsgSender getSender(){
		return sender;
	}
	public HashMap<Double, FloodWarning> getAlertTable(){
		return alertTable;
	}
	
	public void setAlertTable(HashMap<Double, FloodWarning> m){
		alertTable = m;
	}
	/**
	 * Broadcasts the alerts to subscribers.
	 * @param alert
	 */
	public void broadcasting(FloodWarning alert){
		sender.sendGroupWarning(alert);
		System.out.println("Alert is sent at time: "+RepastEssentials.GetTickCount()+ alert.toString());
	}
	
	/**
	 * EA agent's behaviours: monitor the flood.
	 */
	@ScheduledMethod(start =1, interval = 1, priority =20)
	public void monitorFlood(){
		double clock = RepastEssentials.GetTickCount();
//		notFloodedLocations.clear();
//		lightFloodedLocations.clear();
//		mediumFloodedLocations.clear();
//		heavyFloodedLocations.clear();
		if(clock==8.0){
		FloodWarning a1= new FloodWarning("Flood Alert","Flooding is possible. Be prepared");			
		a1.setGenerationTime(RepastEssentials.GetTickCount());
		alertTable.put(clock,a1);
		broadcasting(a1);
		}
		
		/*
		for (Flood f: BuildModel.floodContext){
			if(f.getInundationLevel()==0){
				//do nothing
				notFloodedLocations.add(f.getLocation());
			}
			if(f.getRoadWater()>0 && f.getRoadWater()<=0.5){
				lightFloodedLocations.add(f.getLocation());
			}
			if(f.getRoadWater()>0.5 && f.getRoadWater()<=1){
				mediumFloodedLocations.add(f.getLocation());
			}
			if(f.getRoadWater()>1){
				heavyFloodedLocations.add(f.getLocation());
			}
		}
		
		//the locations might be marked out in graphic interface later

		// if more than 10% of locations are lightly flooded, generate an alert
		if((double)lightFloodedLocations.size()>(double)BuildModel.floodContext.size()/10){
			FloodWarning a2= new FloodWarning("Flood Warning","Flooding is possible. Be prepared");			
			a2.setGenerationTime(RepastEssentials.GetTickCount());
			alertTable.put(clock,a2);
			broadcasting(a2);			
		}
		
		// if more than 5% of locations are medium flooded, generate an alert
		if((double)mediumFloodedLocations.size()>(double)BuildModel.floodContext.size()/5){
			FloodWarning a3= new FloodWarning("Flood Warning","Flooding is expected. Immediate action required");			
			a3.setGenerationTime(RepastEssentials.GetTickCount());
			alertTable.put(clock,a3);
			broadcasting(a3);
		}
		
		// if more than 10% of locations are heavily flooded, generate an alert
		if((double)heavyFloodedLocations.size()>(double)BuildModel.floodContext.size()/10){
			FloodWarning a4= new FloodWarning("Severe Flood Warning","Svere flooding. Danger to life.");			
			a4.setGenerationTime(RepastEssentials.GetTickCount());
			alertTable.put(clock,a4);
			broadcasting(a4);

		}
		*/
		
		
	}
	
	

}
